package com.shwread.android.qysw.activity.task;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.NewActionHelp;
import com.ciko.code.base.BaseActicity;
import com.ciko.code.utils.NetWorkUtils;
import com.ciko.code.utils.Utils;
import com.google.gson.reflect.TypeToken;
import com.shwread.android.qysw.QyReaderApp;
import com.shwread.android.qysw.adapter.TaskAdapter;
import com.shwread.android.qysw.bean.task.TaskActBean;
import com.shwread.android.qysw.bean.task.TaskActBean.TaskActInfo;
import com.shwread.android.qysw10000241.R;
import com.shwread.android.ui.dialog.NetworkDialog;
import com.shwread.http.base.ObjectHttpResponseHandler;

import org.apache.http.Header;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import logic.util.Util;

/**
 * 任务搜索
 */
public class SearchTaskActivity extends BaseActicity implements TaskAdapter.ItemListener {

    /**
     * 搜索
     */
    private EditText etSearchKeyword;
    /**
     * 搜索列表
     */
    private RecyclerView rlvSearchTaskList;
    /**
     * 清空按钮
     */
    private ImageView imgSearchDelete;
    /**
     * 动画页面
     */
    private RelativeLayout commonLoadingLayout;
    /**
     * 加载动画
     */
    private ImageView loadingImg;
    private List<TaskActInfo> mData;
    private TaskAdapter mAdapter;
    /**
     * 搜索按钮
     */
    private Button btSearchContent;

    @Override
    protected void initTopBar() {
        setTitle("任务搜索");
        enableNavLeftImg();
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_search_task);
    }

    @Override
    protected void initView() {
        etSearchKeyword = findViewByID(R.id.et_search_keyword);
        rlvSearchTaskList = findViewByID(R.id.rlv_search_task_list);
        imgSearchDelete = findViewByID(R.id.img_search_delete);
        btSearchContent = findViewByID(R.id.bt_search_content);
        loadingImg = findViewByID(R.id.common_loading__img);
        commonLoadingLayout = findViewByID(R.id.common_loading_layout);
    }

    @Override
    protected void initAdapter() {

    }

    @Override
    protected void initDates() {
        mData = new ArrayList<>();
        mAdapter = new TaskAdapter(mData, this);
        rlvSearchTaskList.setAdapter(mAdapter);
    }

    @Override
    protected void initListener() {
        etSearchKeyword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {//搜索
                getData(etSearchKeyword.getText().toString());
                return true;
            }
        });
        //监听搜索框内容变化
        etSearchKeyword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {//输入文字之前的状态
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {//输入过程中的状态
            }

            @Override
            public void afterTextChanged(Editable editable) {//输入结束后的状态
                imgSearchDelete.setVisibility(editable.toString().isEmpty() ? View.GONE : View.VISIBLE);
                btSearchContent.setVisibility(editable.toString().isEmpty() ? View.GONE : View.VISIBLE);
            }
        });
        imgSearchDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//清除
                imgSearchDelete.setVisibility(View.GONE);
                etSearchKeyword.setText("");
            }
        });
        btSearchContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData(etSearchKeyword.getText().toString());
            }
        });
    }

    @Override
    public void itemClick(TaskActInfo taskBean) {//列表点击事件
        Intent intent = new Intent();
        if (taskBean.getListType() == TaskAdapter.ORGANIZATION_TASK) {
            intent.setClass(this, OrganizationTaskDetailActivity.class);
        } else if (taskBean.getListType() == TaskAdapter.PERSONAL_TASK) {
            intent.setClass(this, PersonalTaskDetailActivity.class);
        }
        intent.putExtra("taskId", taskBean.getId());
        startActivity(intent);
    }

    /**
     * 获取搜索内容
     */
    private void getData(String text) {
        if (TextUtils.isEmpty(text)) {
            Util.showToast(this, "搜索内容不能为空");
            return;
        }
        //隐藏键盘
        Utils.hideInputMethodManager(SearchTaskActivity.this);
        if (!NetWorkUtils.isConnected(this)) {
            new NetworkDialog(this).show();
            return;
        }
        progressDialog.show();
        NewActionHelp.qryPostActivityList(this, 0, 50, text, TaskAdapter.SEARCH_TASK, new ObjectHttpResponseHandler<TaskActBean>() {
            @Override
            public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                Log.e("sss", "失败--" + s);
                isShow(3);
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
            }

            @Override
            public void onSuccess(int i, Header[] headers, TaskActBean taskActBean, String s) {
                Log.e("sss", "成功--" + s);
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
                if (taskActBean != null
                        && taskActBean.getPostActivityInfos() != null
                        && taskActBean.getPostActivityInfos().size() != 0) {
                    mData = taskActBean.getPostActivityInfos();
                    mAdapter.notifyData(mData);
                    isShow(1);
                } else {
                    Log.e("sss", "没有数据");
                    Util.showToast(QyReaderApp.application, "未搜索到内容");
                    isShow(2);
                }
            }

            @Override
            public Type getType() {
                return new TypeToken<TaskActBean>() {
                }.getType();
            }
        });
    }

    /**
     * 动画显示和隐藏
     * @param type 1-加载成功 2-没有数据 3-加载失败
     */
    private void isShow(int type) {
        switch (type) {
            case 1://加载成功
                rlvSearchTaskList.setVisibility(View.VISIBLE);
                commonLoadingLayout.setVisibility(View.GONE);
                break;
            case 2://没有数据
                rlvSearchTaskList.setVisibility(View.GONE);
                commonLoadingLayout.setVisibility(View.VISIBLE);
                commonLoadingLayout.setEnabled(true);
                loadingImg.setBackgroundResource(R.drawable.message_empty_img);
                break;
            case 3://加载失败
                rlvSearchTaskList.setVisibility(View.GONE);
                commonLoadingLayout.setVisibility(View.VISIBLE);
                commonLoadingLayout.setEnabled(true);
                loadingImg.setBackgroundResource(R.drawable.home_loading_fail);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        NewActionHelp.cancel(this);
    }
}
